﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class ProfilePage : System.Web.UI.Page
{
    private UserSub userSub= new UserSub();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            userSub = SubsciptionAccess.GetUserSubscription(User.Identity.Name);
            if (userSub.idSubscription == 0)
            {
                mvSubscription.SetActiveView(unsigned);
                lblPretplataObnovi.Text = "Немате претплата?";
            }
            else
            {
                mvSubscription.SetActiveView(signed);
                int days;
                if ((days = CalculateDays(userSub)) != -1)
                {
                    lblPretplata.Text = "претплатата ви истекува за " + days.ToString() + " дена";
                }
                else
                {
                    mvSubscription.SetActiveView(unsigned);
                    lblPretplataObnovi.Text = "Обновете ја претплатата";
                    SubsciptionAccess.SetSubscriptionBitToFalse(User.Identity.Name);
                }
            }
        }
    }
    
    private int CalculateDays(UserSub us)
    {
        int length=0;
        switch (us.idSubscription)
        {
            //1 mesec
            case 1:
                length = 30;
                break;
            //3 meseci
            case 2:
                length = 90;
                break;
            //1 godina
            case 4:
                length = 365;
                break;
            default:
                break;
        }
        DateTime today = DateTime.Today;
        DateTime subsribed = us.DateSubscribed;
        TimeSpan ts = today - subsribed;
        if (ts.Days < length)
        {
            return length - ts.Days;
        }
        else
        {
            return -1;
        }
        
    }

    protected void btnSub_Click(object sender, EventArgs e)
    {
        //Nema potreba od pravenje na order AMA ke vidime
        //namesti go da se odbira tip na subscription
        Response.Redirect("Subscription.aspx");
    }
}